package com.hefei.blog.web.auth.service.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hefei.blog.base.cache.UserEhcacheManager;
import com.hefei.blog.core.data.AccountProfile;
import com.hefei.blog.core.persist.service.UserService;
import com.hefei.blog.web.auth.service.IAuthService;
import com.hefei.common.returncode.ReturnCode;
import com.hefei.common.util.encrypt.MD5;
import com.hefei.common.util.encrypt.MD5Version;
import com.hefei.frontend.framework.base.CookieConstants;
import com.hefei.frontend.framework.base.UserCookieUtil;
import com.hefei.frontend.framework.base.user.UserCookie;
import com.hefei.frontend.framework.cookie.util.CookieUtil;
import com.hefei.frontend.framework.http.request.RequestThreadLocal;

@Service
public class AuthService implements IAuthService{
	private Logger logger = Logger.getLogger(AuthService.class);
	
	@Autowired
	private UserService userService;
	
	@Override
	public String login(String loginName,String password,HttpServletRequest request,HttpServletResponse response) {
		String returnCode = ReturnCode.RETURN_CODE_ERROR;
		try {
			
			
			AccountProfile profile = userService.login(loginName, MD5.MD5Encrypt(password,MD5Version.V1.getVersion()));
			if(profile!=null){
				writeLoginCookie(profile, request, response);
				putProfile(profile);
				
				returnCode = ReturnCode.RETURN_CODE_SUCCESS;
			}
		} catch (Exception e) {
			logger.error("error:",e);
		}
		return returnCode;
	}

	public String loginByToken(String loginName,String accessToken,HttpServletRequest request,HttpServletResponse response) {
		String returnCode = ReturnCode.RETURN_CODE_ERROR;
		try {
			AccountProfile profile = userService.getProfileByName(loginName);
			if(profile!=null){
				writeLoginCookie(profile, request, response);
				putProfile(profile);
				returnCode = ReturnCode.RETURN_CODE_SUCCESS;
			}
		} catch (Exception e) {
			logger.error("error:",e);
		}
		return returnCode;
	}
	
	public void putProfile(AccountProfile profile){
		UserEhcacheManager.putAccountProfile(profile);
	}
	
	public AccountProfile getProfile(Long userId){
		return UserEhcacheManager.getAccountProfile(userId);
	}
	
	public void writeLoginCookie(AccountProfile profile, HttpServletRequest request,HttpServletResponse response) throws Exception{
		try {
			UserCookie userCookie = new UserCookie();
			userCookie.setUserId(profile.getId());
			userCookie.setAvatarPath(profile.getAvatar());
			userCookie.setNickname(profile.getName());
			
			UserCookieUtil.setUserCookie(request, response, CookieConstants.COOKIE_DOMAIN, CookieConstants.COOKIE_NAME_WEB, userCookie, CookieConstants.COOKIE_AGE_SESSION, true, false);
			CookieUtil.setCookie(request, response, CookieConstants.COOKIE_DOMAIN, CookieConstants.COOKIE_NAME_USER_LOGIN_USERID, userCookie.getUserId().toString(), CookieConstants.COOKIE_AGE_SESSION, true, false);
		
			CookieUtil.setCookie(request, response, CookieConstants.COOKIE_DOMAIN, CookieConstants.COOKIE_NAME_USER_LOGIN_NICKNAME, userCookie.getNickname(), CookieConstants.COOKIE_AGE_ONE_WEEK, true, false);
			
		} catch (Exception e) {
			logger.error(RequestThreadLocal.getRequestStr()+"error:",e);
			throw e;
		}
	}
	
}
